package com.sk.bilibili.model.fragment.main.home.dynamic;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.DynamicMapper;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.PraiseMapper;
import com.sk.bilibili.model.BaseRefreshModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.fragment.main.home.dynamic.SynthesizeFragment;

public class SynthesizeModel extends BaseRefreshModel<SynthesizeFragment> {

    @Autowired
    DynamicMapper dynamicMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    FocusMapper focusMapper;
    private int page=1;
    private int size=10;

    @Override
    public void refreshData() {
        page=1;
        JsonMap map=getJsonMap();
        RequestUtils.process(dynamicMapper.getDynamic(map), dynamicPage -> {
            page++;
            view.refreshSuccess(dynamicPage.getRecords());
        });
    }

    private JsonMap getJsonMap() {
        JsonMap map = new JsonMap();
        map.put("size",size);
        map.put("page",page);
        return map;
    }

    @Override
    public void loadData() {
        JsonMap map=getJsonMap();
        RequestUtils.process(dynamicMapper.getDynamic(map), dynamicPage -> {
            page++;
            view.loadSuccess(dynamicPage.getRecords());
        });
    }

    public void praise(int type, Integer targetId) {
        JsonMap map = new JsonMap();
        map.put("type",type);
        map.put("targetId",targetId);
        RequestUtils.process(praiseMapper.switchPraise(map),
                aBoolean -> view.praiseSuccess());
    }

    public void switchFocus(Integer id) {
        RequestUtils.process(focusMapper.switchFocus(id),
                focus -> view.switchFocusSuccess(focus));
    }

}